﻿local RADIO_ELEMENTS = {}
local NEW_RADIO_ADD = {}

addEventHandler("onClientResourceStart", getResourceRootElement(), function()
	load_radio_gui()
	LOAD_RADIO_ITEMS()
	new_radio_add_build()
	guiSetVisible(RADIO_ELEMENTS["_root"], false)
	bindKey("F5", "down", function()
		if guiGetVisible(RADIO_ELEMENTS["_root"]) then
			guiSetVisible(RADIO_ELEMENTS["_root"], false)
			showCursor(false)
			guiSetVisible(NEW_RADIO_ADD["_root"], false)
			guiSetText(NEW_RADIO_ADD["Testen"], "")
			guiSetText(NEW_RADIO_ADD["Hinzufugen"], "")
		else
			guiSetVisible(RADIO_ELEMENTS["_root"], true)
			showCursor(true)
		end
	end)
end)

function load_radio_gui()
	RADIO_ELEMENTS._placeHolders = {}
	
	local screenWidth, screenHeight = guiGetScreenSize()
	local windowWidth, windowHeight = 461, 295
	local left = screenWidth/2 - windowWidth/2
	local top = screenHeight/2 - windowHeight/2
	RADIO_ELEMENTS["_root"] = guiCreateWindow(left, top, windowWidth, windowHeight, "Butterfly Radio - GUI", false)
	guiWindowSetSizable(RADIO_ELEMENTS["_root"], false)
	
	RADIO_ELEMENTS["tableWidget"] = guiCreateGridList(10, 25, 441, 231, false, RADIO_ELEMENTS["_root"])
	guiGridListSetSortingEnabled(RADIO_ELEMENTS["tableWidget"], false)
	RADIO_ELEMENTS["tableWidget_col0"] = guiGridListAddColumn(RADIO_ELEMENTS["tableWidget"], "Radiosender - Name", 0.4)
	RADIO_ELEMENTS["tableWidget_col1"] = guiGridListAddColumn(RADIO_ELEMENTS["tableWidget"], "Radiosender - URL", 0.6)
	
	RADIO_ELEMENTS["pushButton"] = guiCreateButton(380, 265, 75, 23, "OK", false, RADIO_ELEMENTS["_root"])
	if on_pushButton_clicked then
		addEventHandler("onClientGUIClick", RADIO_ELEMENTS["pushButton"], on_pushButton_clicked, false)
	end
	
	RADIO_ELEMENTS["pushButton_2"] = guiCreateButton(10, 265, 21, 23, "+", false, RADIO_ELEMENTS["_root"])
	if on_pushButton_2_clicked then
		addEventHandler("onClientGUIClick", RADIO_ELEMENTS["pushButton_2"], on_pushButton_2_clicked, false)
	end
	
	RADIO_ELEMENTS["pushButton_3"] = guiCreateButton(30, 265, 21, 23, "-", false, RADIO_ELEMENTS["_root"])
	if on_pushButton_3_clicked then
		addEventHandler("onClientGUIClick", RADIO_ELEMENTS["pushButton_3"], on_pushButton_3_clicked, false)
	end
	
	return RADIO_ELEMENTS, windowWidth, windowHeight
end

function on_pushButton_clicked(button, state, absoluteX, absoluteY)
	if (button ~= "left") or (state ~= "up") then
		return
	end
	
	if guiGetVisible(RADIO_ELEMENTS["_root"]) then
		BUTTERFLY_RADIO = nil
		BUTTERFLY_RADIO = {}
		BUTTERFLY_RADIO_PATHS = nil
		BUTTERFLY_RADIO_PATHS = {}
		for i=0, guiGridListGetRowCount(RADIO_ELEMENTS["tableWidget"]) do
			--BUTTERFLY_RADIO[i] = guiGridListGetItemText(RADIO_ELEMENTS["tableWidget"], i, 1)
			--BUTTERFLY_RADIO_PATHS[i] = guiGridListGetItemText(RADIO_ELEMENTS["tableWidget"], i, 2)
			table.insert(BUTTERFLY_RADIO,  guiGridListGetItemText(RADIO_ELEMENTS["tableWidget"], i, 1))
			table.insert(BUTTERFLY_RADIO_PATHS,  guiGridListGetItemText(RADIO_ELEMENTS["tableWidget"], i, 2))
		end
		local load_radios = xmlLoadFile ('RADIO/RADIOS/radios.xml')
		if load_radios == false then
			local radios = xmlCreateFile("RADIO/RADIOS/radios.xml", "radios")
			for i=0, guiGridListGetRowCount(RADIO_ELEMENTS["tableWidget"]) do
				local radio = xmlCreateChild(radios, "radio")
				xmlNodeSetAttribute(radio, "radioname", guiGridListGetItemText(RADIO_ELEMENTS["tableWidget"], i, 1))
				xmlNodeSetAttribute(radio, "radiourl", guiGridListGetItemText(RADIO_ELEMENTS["tableWidget"], i, 2))
			end
			xmlSaveFile(radios)
		end
		
		guiSetVisible(RADIO_ELEMENTS["_root"], false)
		showCursor(false)
	else
		guiSetVisible(RADIO_ELEMENTS["_root"], true)
		showCursor(true)
	end
end

function on_pushButton_2_clicked(button, state, absoluteX, absoluteY)
	if (button ~= "left") or (state ~= "up") then
		return
	end
	
	guiSetVisible(NEW_RADIO_ADD["_root"], true)
	guiBringToFront(NEW_RADIO_ADD["_root"])
	guiSetEnabled(NEW_RADIO_ADD["Hinzufugen"], false)
end

function on_pushButton_3_clicked(button, state, absoluteX, absoluteY)
	if (button ~= "left") or (state ~= "up") then
		return
	end
	local zeile, spalte = guiGridListGetSelectedItem(RADIO_ELEMENTS["tableWidget"])
	if zeile == 0 then outputChatBox("Du Knecht, wie willst du Vollidiot dann dein Radio ausmachen?", 255, 100, 0) return end
	guiGridListRemoveRow(RADIO_ELEMENTS["tableWidget"], zeile)
end

function new_radio_add_build()
	NEW_RADIO_ADD._placeHolders = {}
	
	local screenWidth, screenHeight = guiGetScreenSize()
	local windowWidth, windowHeight = 312, 115
	local left = screenWidth/2 - windowWidth/2
	local top = screenHeight/2 - windowHeight/2
	NEW_RADIO_ADD["_root"] = guiCreateWindow(left, top, windowWidth, windowHeight, "Neues Radio hinzufügen", false)
	guiWindowSetSizable(NEW_RADIO_ADD["_root"], false)
	
	NEW_RADIO_ADD["Testen"] = guiCreateButton(150, 85, 75, 23, "Testen", false, NEW_RADIO_ADD["_root"])
	if on_pushButton_clicked then
		addEventHandler("onClientGUIClick", NEW_RADIO_ADD["Testen"], on_testen_clicked, false)
	end
	
	NEW_RADIO_ADD["Hinzufugen"] = guiCreateButton(230, 85, 75, 23, "Hinzufugen", false, NEW_RADIO_ADD["_root"])
	if on_pushButton_2_clicked then
		addEventHandler("onClientGUIClick", NEW_RADIO_ADD["Hinzufugen"], on_add_2_clicked, false)
	end
	
	NEW_RADIO_ADD["label"] = guiCreateLabel(20, 25, 131, 16, "Radiosender - Name:", false, NEW_RADIO_ADD["_root"])
	guiLabelSetHorizontalAlign(NEW_RADIO_ADD["label"], "left", false)
	guiLabelSetVerticalAlign(NEW_RADIO_ADD["label"], "center")
	
	NEW_RADIO_ADD["label_2"] = guiCreateLabel(20, 55, 131, 16, "Radiosender - URL:", false, NEW_RADIO_ADD["_root"])
	guiLabelSetHorizontalAlign(NEW_RADIO_ADD["label_2"], "left", false)
	guiLabelSetVerticalAlign(NEW_RADIO_ADD["label_2"], "center")
	
	NEW_RADIO_ADD["RADIO_NAME"] = guiCreateEdit(152, 25, 151, 20, "", false, NEW_RADIO_ADD["_root"])
	
	NEW_RADIO_ADD["RADIO_URL"] = guiCreateEdit(150, 55, 151, 20, "", false, NEW_RADIO_ADD["_root"])
	
	guiSetVisible(NEW_RADIO_ADD["_root"], false)
	
	return gui, windowWidth, windowHeight
end

function on_testen_clicked(button, state, absoluteX, absoluteY)
	if (button ~= "left") or (state ~= "up") then
		return
	end
	if not (guiGetText(NEW_RADIO_ADD["RADIO_NAME"]) == "") and not (guiGetText(NEW_RADIO_ADD["RADIO_URL"]) == "")  then
		local TEST_RADIO_SOUND = playSound(guiGetText(NEW_RADIO_ADD["RADIO_URL"]))
		if TEST_RADIO_SOUND then
			outputChatBox("Dieser Radiosender funktioniert einwandfrei!", 0, 255, 0)
			guiSetEnabled(NEW_RADIO_ADD["Hinzufugen"], true)
			--setTimer(function() stopSound(TEST_RADIO_SOUND) end, 15, 1)
			setTimer(stopSound, 15000, 1, TEST_RADIO_SOUND)
		else
			outputChatBox("Dieser Radiosender funktioniert leider nicht! Bitte beachte deine URL-Angabe!", 255, 0, 0)
		end
	else
		outputChatBox("Du hast keine gültigen Angaben gemacht!", 255, 50, 0)
	end
end

function on_add_2_clicked(button, state, absoluteX, absoluteY)
	if (button ~= "left") or (state ~= "up") then
		return
	end
	local tableWidget_row = guiGridListAddRow(RADIO_ELEMENTS["tableWidget"])
	guiGridListSetItemText(RADIO_ELEMENTS["tableWidget"], tableWidget_row, RADIO_ELEMENTS["tableWidget_col0"], guiGetText(NEW_RADIO_ADD["RADIO_NAME"]), false, false )
	guiGridListSetItemText(RADIO_ELEMENTS["tableWidget"], tableWidget_row, RADIO_ELEMENTS["tableWidget_col1"], guiGetText(NEW_RADIO_ADD["RADIO_URL"]), false, false )
	guiSetText(NEW_RADIO_ADD["RADIO_NAME"], "")
	guiSetText(NEW_RADIO_ADD["RADIO_URL"], "")
	guiSetVisible(NEW_RADIO_ADD["_root"], false)
end

function LOAD_RADIO_ITEMS()
	for index, radioname in ipairs(BUTTERFLY_RADIO) do
		local tableWidget_row = guiGridListAddRow(RADIO_ELEMENTS["tableWidget"])
		guiGridListSetItemText(RADIO_ELEMENTS["tableWidget"], tableWidget_row, RADIO_ELEMENTS["tableWidget_col0"], radioname, false, false )
	end
	
	for index, radiourl in ipairs(BUTTERFLY_RADIO_PATHS) do
		--local tableWidget_row = guiGridListAddRow(RADIO_ELEMENTS["tableWidget"])
		if radiourl == 0 then radiourl = "AUS" end
		guiGridListSetItemText(RADIO_ELEMENTS["tableWidget"], index-1, RADIO_ELEMENTS["tableWidget_col1"], radiourl, false, false )
	end
end
